﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServer.Classes.Utility;

namespace WcfServer.Classes.Server
{
    /*
     * MUST READ: User SignUpUserInfo to fill out info and then prepare.
     * On server it should receive a SignUpRequest as a result and then construct SignUpUserInfo back using the byte[] in SignUpRequest
     * (a better way?)
     * */
    [Serializable]
    public class SignUpUserInfo
    {
        public String FirstName { set; get; }
        public String LastName { set; get; }
        public byte[] Avatar { set; get; } // add this later.
        public String UserID { set; get; }
        public byte[] Password { set; get; } //hashed!

        public byte[] PublicKeyHash { set; get; }

        public SignUpUserInfo() { }

        public SignUpUserInfo(byte[] data)
        {
            var t = SerializerHandler<SignUpUserInfo>.DeSerializeInput(data);
            this.FirstName = t.FirstName;
            this.LastName = t.LastName;
            this.Avatar = t.Avatar;
            this.UserID = t.UserID;
            this.Password = t.Password;
            t.PublicKeyHash = t.PublicKeyHash;
        }
        public byte[] Prepare(string serverPublicKey)
        {
            KeyHandler.genKey_SaveInContainer(UserID);

            String publicKey = KeyHandler.getKey_XML(UserID, false);

            PublicKeyHash = EncryptionHandler.HashSHA256(SerializerHandler<String>.SerializeInput(publicKey));

            byte[] encryptedInfo = EncryptionHandler.EncryptRSA_XML(SerializerHandler<SignUpUserInfo>.SerializeInput(this),serverPublicKey);

            return (new SignUpRequest(encryptedInfo, publicKey).Prepare());


        }
    }
    [Serializable]
    public class SignUpRequest
    {

        public byte[] userInfo;
        public String PublicKey;


        //TODO: SECURE HERE AS WELL USING SERVER PUBLIC KEY.
        public SignUpRequest(byte[] data)
        {
            var r = SerializerHandler<SignUpRequest>.DeSerializeInput(data);
            this.userInfo = r.userInfo;
            this.PublicKey = r.PublicKey;
        }

        public SignUpRequest()
        {

        }

        public SignUpRequest(byte[] encInfo, String pub)
        {
            userInfo = encInfo; PublicKey = pub;
        }

        public byte[] Prepare()
        {
            return SerializerHandler<SignUpRequest>.SerializeInput(this);
        }
    }
}
